<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\system\BannerModel;
use think\Request;

/**
 * CompanyBanner
 * Class CompanyBanner
 * @package app\controller
 */
class CompanyBanner extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new BannerModel();
    }

    /**
     * 分页列表
     * @param string $status
     * @param string $jump_type
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @param int $area
     * @return \think\response\Json
     */
    public function getPage($status = '', $jump_type = '', $keyword = '', $page = 1, $limit = 10, $area = 'comindex')
    {
        $data = $this->model->getPageData($status, $jump_type, $keyword, $page, $limit, "*", $area);
        $dataList = $data['dataList'];
        foreach ($dataList as $value) {
            $value['start_time'] = date("Y-m-d H:i:s", $value['start_time']);
            $value['end_time'] = date("Y-m-d H:i:s", $value['end_time']);
        }
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $dataList
        ]);
    }

    /**
     * 单条信息
     * @param $id
     * @return \think\response\Json
     */
    public function getInfo($id)
    {
        $banner = $this->model->getInfo($id);
        $banner['start_time'] = $banner['start_time'] * 1000;
        $banner['end_time'] = $banner['end_time'] * 1000;
        if (!empty($banner['banner_detail'])) {
            $banner['banner_detail'] = explode(',', $banner['banner_detail']);
        } else {
            $banner['banner_detail'] = [];
        }
        if (!empty($banner)) {
            return self::returnMsg(1, $banner);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 所有列表
     * @return \think\response\Json
     */
    public function getAll()
    {
        $bannerList = [];
        $dataList = $this->model->getAllByRedisRank();
        foreach ($dataList as $obj) {
            array_push($bannerList, [
                'banner_title' => $obj['banner_title'],
                'banner_src' => $obj['banner_src'],
                'jump_type' => $obj['jump_type'],
                'area' => $obj['area'],
                'jump_link' => $obj['jump_link']
            ]);
        }
        return self::returnMsg(1, ['bannerList' => $bannerList]);
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'banner_title' => $request->param('banner_title'),
            'banner_src' => $request->param('banner_src'),
            'banner_detail' => $request->param('banner_detail'),
            'jump_type' => $request->param('jump_type'),
            'jump_link' => $request->param('jump_link'),
            'area' => $request->param('area'),
            'goods_id' => $request->param('goods_id'),
            'status' => $request->param('status'),
            'start_time' => $request->param('start_time'),
            'end_time' => $request->param('end_time'),
            'rank' => $request->param('rank')
        ];
        if (empty($formData['id'])) {
            $validate = 'app\validate\BannerValidate.add';
        } else {
            $validate = 'app\validate\BannerValidate';
        }
        $result = $this->validate($formData, $validate);

        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            if (empty($formData['start_time'])) {
                $formData['start_time'] = time();
            } else {
                $formData['start_time'] = $formData['start_time'] / 1000;
            }
            if (empty($formData['end_time'])) {
                $formData['end_time'] = time() + (3600 * 24 * 365);
            } else {
                $formData['end_time'] = $formData['end_time'] / 1000;
            }
            $ret = $this->model->saveData($formData, true, true);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'BannerModel', 'Banner管理', $this->currentUser['user_id'], $this->currentUser['user_type'], 'system');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }

    /**
     * 保存状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function onStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'status' => $request->param('status'),
        ];
        $validate = 'app\validate\BannerValidate.upstatus';
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, true, true);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }
}
